﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BPM.Foundation.PWDB;
namespace BPM.Foundation.PWService
{
    public partial interface IPowerService
    {
        [System.ServiceModel.OperationContract]
        List<PW_Mode> getModeList(string pid);

          [System.ServiceModel.OperationContract]
        int? addModePower(string pid, PW_ModePower pw_ModePower);

          [System.ServiceModel.OperationContract]
        int? removeModePower(string pid, PW_ModePower pw_ModePower);

          [System.ServiceModel.OperationContract]
          List<string> getUserModeList(string pid, string userNo, string departmentNo, string dutyNo);

          [System.ServiceModel.OperationContract]
          List<PW_ModePower> getModeUserList(string pid, string modeID);
    }

    public partial class PowerService : IPowerService
    {
        //[9.1	得到模块列表]
        public List<PW_Mode> getModeList(string pid)
        {
            return PWDB.PW_Mode.ToList();
        }

        //[9.2	添加模块权限]
        public int? addModePower(string pid, PW_ModePower pw_ModePower)
        {

            if (PWDB.PW_ModePower.Count(p => p.ModeID == pw_ModePower.ModeID && p.userNo == pw_ModePower.userNo && p.departmentNo == pw_ModePower.departmentNo && p.dutyNo == pw_ModePower.dutyNo) != 0)
            {
                return 1;
            }
            pw_ModePower.ID = System.Guid.NewGuid();
            PWDB.PW_ModePower.InsertOnSubmit(pw_ModePower);

            this.submit();
            return null;
        }


        //[9.3	删除模块权限]
        public int? removeModePower(string pid, PW_ModePower pw_ModePower)
        {
            var id = pw_ModePower.ID;

            if (PWDB.PW_ModePower.Count(p => p.ID == id) != 1)
            {
                return 1;
            }
           
           var v= PWDB.PW_ModePower.Single (p => p.ID == id);

           PWDB.PW_ModePower.DeleteOnSubmit(v);

            this.submit();
            return null;
        }


        //[9.4 用户可用模块列表]
        public List<string> getUserModeList(string pid, string userNo, string departmentNo, string dutyNo)
        {
            List<string> modeList = new List<string>();

            var v = PWDB.PW_ModePower.Where(p => p.userNo == userNo && p.departmentNo == departmentNo && p.dutyNo == dutyNo).ToList();

            modeList = v.Select(p => p.ModeID).ToList();

            return modeList;

        }

        //[9.5 模块对应用户列表]
        public List<PW_ModePower> getModeUserList(string pid, string modeID)
        {
            return PWDB.PW_ModePower.Where(p => p.ModeID == modeID).ToList();

        }
    }
}